from multiprocessing import Process, Queue
from selenium import webdriver
import csv
import time

##### Begining Time #####
now = time.localtime()
s = "%04d-%02d-%02d %02d:%02d:%02d" % (now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)
print("Start :" + str(s))


##### Setting selenium driver object #####
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1920x1080')
options.add_argument("disable-gpu")
driver = webdriver.Chrome('C:\\chromedrive\\chromedriver', options = options)
driver.implicitly_wait(3)


##### Setting csv object #####
f = open("C:\\KR Patent Project\\Corp_KIPRIS.csv", "w", newline='')
wf = csv.writer(f)
wf.writerow(["firmname", "engname" ,"residnumber", "KIPRIS ID"])
csvObj = open("C:\\KR Patent Project\\dataguide.csv", "r", encoding = "utf-8")
dataguide = csv.reader(csvObj)
linelist = list(dataguide)
length = len(linelist)




##### Downloader #####
def downloader(linelist, q):
    for line in linelist:
        kname = line[1]
        engname = line[2]
        if len(line[4]) < 13:
            pass
        else :
            resid1 = line[4][:6]
            resid2 = line[4][-8:]
            driver.get("https://www.patent.go.kr/jsp/kiponet/mp/apagtinfo/ReadApAgtInfoInput.jsp")
            element = driver.find_element_by_id("rd_2")
            driver.execute_script("arguments[0].click();", element)
            driver.find_element_by_name('resid1').send_keys(str(resid1))
            driver.find_element_by_name('resid2').send_keys(str(resid2))
            try: 
                element1 = driver.find_elements_by_xpath("//div[@class='searchCat_1']/div[@id='search_rd2']/div[@class='searchBtn']/a")[0]
                driver.execute_script("arguments[0].click();", element1)
                try:
                    ##############################################
                    a =driver.find_elements_by_xpath("//*[@id='ReadApAgtInfoByPrivate']/tbody/tr/td")
                    number = len(a)
                    for i in range(number):
                        ID = driver.find_element_by_xpath("//*[@id='ReadApAgtInfoByPrivate']/tbody/tr[{}]/td[2]".format(i+1))
                        ID = ID.text
                        q.put([str(kname), str(engname), str(line[4]), str(ID)])
                except :
                    q.put([str(kname), str(engname), str(line[4]), str(ID)])
            except :
                pass


##### Writer #####
def writer(q):
    while True:
        data = q.get()
        print('data found to be processed: {}'.format(data))
        wf.writerow(data)
        print('processed')


##### Starting Parallel Computing #####
if __name__ == '__main__':
    q = Queue()
    process_one = Process(target=downloader, args=(linelist[:int(length/3)], q))
    process_two = Process(target=downloader, args=(linelist[int(length/3): 2*int(length/3)], q))
    process_three = Process(target=downloader, args=(linelist[2*int(length/3):], q))
    process_four = Process(target=writer, args=(q,))
    process_one.start()
    process_two.start()
    process_three.start()
    process_four.start()

    q.close()
    q.join_thread()

    process_one.join()
    process_two.join()
    process_three.join()
    process_four.join()
